/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConflitoEventoDialog.java
 *
 * Created on 11/11/2011, 23:16:48
 */
package lp2.projeto.agenda;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JDialog;

import lp2.projeto.anotacao.Anotacao;
import lp2.projeto.gerenciador.Evento;
import lp2.projeto.gerenciador.Gerenciador;
import lp2.projeto.horario.Horario;
import lp2.projeto.horario.Mes;

/**
 *
 * @author Tercio de Melo
 */
public class CriaEventoDialog extends javax.swing.JDialog {
	
	private static final long serialVersionUID = -3017976022180609926L;
	Gerenciador gerenciador;
	private Evento novoEvento;
	private List<Evento> eventosConflitantes;
	private JDialog eventoJDialog;
	protected List<File> listaFile = new ArrayList<File>();

    /** Creates new form ConflitoEventoDialog */
    public CriaEventoDialog(java.awt.Frame parent, boolean modal, Gerenciador gerenciador) {
        super(parent, modal);
        eventoJDialog = this;
        initComponents();
        this.gerenciador = gerenciador;
    }

    private List<byte[]> getImagens() {
    	List<byte[]> resultado = new ArrayList<byte[]>();
    	boolean erro = false;
    	
    	for (File file : listaFile) {
    		try {
    			FileInputStream is = new FileInputStream(file);
    			ArrayList<Integer> arquivo = new ArrayList<Integer>();
    		
    			int valor;
    			while ((valor = is.read()) != -1)
    				arquivo.add(valor);
    			
    			byte[] imagem = new byte[arquivo.size()];
    			
    			for (int i = 0; i < arquivo.size(); i++)
    				imagem[i] = arquivo.get(i).byteValue();
    			
    			resultado.add(imagem);
    		} catch (IOException ioex) {
    			erro = true;
    		}
    	}
    	
    	if (erro) {
    		avisoJDialog aviso = new avisoJDialog("Nao foi possivel carregar todas as imagens.");
    		aviso.setVisible(true);
    	}
    	
    	return resultado;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        nomeLabel = new javax.swing.JLabel();
        dataLabel = new javax.swing.JLabel();
        horaLabel = new javax.swing.JLabel();
        duracaoLabel = new javax.swing.JLabel();
        descricaoLabel = new javax.swing.JLabel();
        nomeField = new javax.swing.JTextField();
        diasList = new javax.swing.JComboBox();
        mesesList = new javax.swing.JComboBox();
        anosLists = new javax.swing.JComboBox();
        horasList = new javax.swing.JComboBox();
        duracaoField = new javax.swing.JTextField();
        minutosList = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        descricaoField = new javax.swing.JTextArea();
        cancelarButton = new javax.swing.JButton();
        salvarButton = new javax.swing.JButton();
        imagemButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        tipoLabel = new javax.swing.JLabel();
        tipoField = new javax.swing.JTextField();
        warning = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        nomeLabel.setText("Nome do Evento:");

        dataLabel.setText("Data:");

        horaLabel.setText("Hora:");

        duracaoLabel.setText("Duracao:");

        descricaoLabel.setText("Descricao:");

        nomeField.setText("Nome do Evento");

        diasList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        mesesList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Janeiro", "Fevereiro", "Marco", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro" }));

        anosLists.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026", "2027", "2028", "2029", "2030", "2031", "2032", "2033", "2034", "2035", "2036", "2037", "2038", "2039", "2040", "2041", "2042", "2043", "2044", "2045", "2046", "2047", "2048", "2049", "2050" }));

        horasList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }));

        duracaoField.setText("minutos");

        minutosList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" }));

        descricaoField.setColumns(20);
        descricaoField.setRows(5);
        jScrollPane1.setViewportView(descricaoField);

cancelarButton.setText("Cancelar");
        
        cancelarButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

        salvarButton.setText("Salvar"); 
        
        salvarButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					String nome = nomeField.getText();
					int ano = Integer.parseInt((String) anosLists.getSelectedItem());
					Mes mes = Mes.values()[mesesList.getSelectedIndex()];
					int dia = diasList.getSelectedIndex() + 1;
					int hora = horasList.getSelectedIndex();
					int minuto = minutosList.getSelectedIndex();
					int duracao = Integer.parseInt(duracaoField.getText());
					
					String tipo = tipoField.getText();
					Horario horario = new Horario(ano, mes, dia, hora, minuto);
					
					novoEvento = new Evento(nome, horario, descricaoField.getText(), duracao, tipo);
					
					//novoEvento.setAnotacoes(getImagens());
					//@TODO Vai ter que arrumar isso
					
					eventosConflitantes = new ArrayList<Evento>();
					for (Evento evento : gerenciador.getEventos())
						if (evento.getIntervalo().colideCom(novoEvento.getIntervalo()))
							eventosConflitantes.add(evento);
					
					if (eventosConflitantes.size() > 0) {
						ActionListener acaoRemover = new ActionListener() {
							@Override
							public void actionPerformed(ActionEvent e) {
								for (Evento evento : eventosConflitantes)
									gerenciador.removeEvento(evento);
								
								avisoJDialog avisoJDialog = new avisoJDialog("Os eventos conflitantes foram removidos");
								avisoJDialog.setVisible(true);
								
								gerenciador.adicionaEvento(novoEvento);
							}
						};
						ActionListener acaoSobrecarregar = new ActionListener() {
							@Override
							public void actionPerformed(ActionEvent e) {
								gerenciador.adicionaEvento(novoEvento);
							}
						};
						ActionListener acaoDesistir = new ActionListener() {
							@Override
							public void actionPerformed(ActionEvent arg0) {
							}
						};
						new ConflitoEventoJDialog(eventosConflitantes, acaoRemover, acaoSobrecarregar, acaoDesistir);
					} else {
						gerenciador.adicionaEvento(novoEvento);
					}
					
					AgendaGrafica.atualizarListas();
					
					dispose();
				} catch(Exception excpt) {
					warning.setText("Coloque dados validos");
				}
			}
		});

        imagemButton.setText("Imagem");
        
        imagemButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JDialog imagemJDialog = new ImagemJDialog(null,getThis()); 
				imagemJDialog.pack();
				imagemJDialog.setVisible(true);
				
			}
		});

        jButton1.setText("Som");

        tipoLabel.setText("Tipo:");

        tipoField.setText("Tipo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(descricaoLabel)
                        .addGap(43, 43, 43)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nomeLabel)
                            .addComponent(dataLabel)
                            .addComponent(horaLabel)
                            .addComponent(duracaoLabel)
                            .addComponent(tipoLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(imagemButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(salvarButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancelarButton))
                            .addComponent(nomeField, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(horasList, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                    .addComponent(diasList, javax.swing.GroupLayout.Alignment.LEADING, 0, 50, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(mesesList, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(anosLists, 0, 106, Short.MAX_VALUE))
                                    .addComponent(minutosList, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(tipoField, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(duracaoField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
                                .addGap(62, 62, 62)
                                .addComponent(warning)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nomeLabel)
                    .addComponent(nomeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataLabel)
                    .addComponent(diasList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mesesList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(anosLists, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(horaLabel)
                    .addComponent(horasList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(minutosList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(duracaoLabel)
                    .addComponent(duracaoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(warning))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tipoLabel)
                    .addComponent(tipoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cancelarButton)
                            .addComponent(salvarButton)
                            .addComponent(imagemButton)
                            .addComponent(jButton1)))
                    .addComponent(descricaoLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    public void setListaFile(List<File> listaFile) {
		this.listaFile = listaFile;
	}
    public CriaEventoDialog getThis() {
		return this;
	}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CriaEventoDialog dialog = new CriaEventoDialog(new javax.swing.JFrame(), true, new Gerenciador());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JComboBox anosLists;
    private javax.swing.JButton cancelarButton;
    private javax.swing.JLabel dataLabel;
    private javax.swing.JTextArea descricaoField;
    private javax.swing.JLabel descricaoLabel;
    private javax.swing.JComboBox diasList;
    private javax.swing.JTextField duracaoField;
    private javax.swing.JLabel duracaoLabel;
    private javax.swing.JLabel horaLabel;
    private javax.swing.JComboBox horasList;
    private javax.swing.JButton imagemButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox mesesList;
    private javax.swing.JComboBox minutosList;
    private javax.swing.JTextField nomeField;
    private javax.swing.JLabel nomeLabel;
    private javax.swing.JButton salvarButton;
    private javax.swing.JTextField tipoField;
    private javax.swing.JLabel tipoLabel;
    private javax.swing.JLabel warning;
    // End of variables declaration
}
